package com.lkm.app_ui.ui;


import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.QRCodeUtil;
import com.lkm.app_ui.m.fun.OnWeightHoldEvent;
import com.lkm.app_ui.ui.sale.SaleContract;
import com.lkm.comlib.Config;
import com.lkm.comlib.help.ViewUIHelp;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.ObserverAutoNext;
import com.lkm.frame.task2.CTaskExecutor;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;

import androidx.annotation.Nullable;



@Deprecated 
public class PaySuccessTipsFragment extends MBaseFragment {


    private SaleContract.Presenter presenter;

    public static PaySuccessTipsFragment getInstance(String printTable, String orderurl) {
        PaySuccessTipsFragment f = new PaySuccessTipsFragment();
        Bundle data = new Bundle();
        data.putString("orderurl:s", orderurl);
        data.putString("printTable:s", printTable);
        f.setArguments(data);
        return f;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_sec_pay_tips_success;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter = SaleContract.presenter;







////                    getActivity().getSupportFragmentManager().popBackStackImmediate();

////                    getParentFragment().getChildFragmentManager().popBackStackImmediate();


//                ((DisplaySecondModule)MyApplication.getInstance().getModuleManager().loadModule(DisplaySecondModule.class)).dismiss(PaySuccessTipsFragment.this);



        CreateQR createQR = new CreateQR();
        createQR.setParam(new Object[]{getArguments().getString("orderurl:s")});
        createQR.action();
        createQR.execTask();

        loopD();

        reOnKeyEvent();
        regOnWeightHoldEvent();

        PlayBeepEvent.send(PlayBeepEvent.TYPE_paySuccess);
    }

    private void reOnKeyEvent(){
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnSteelyardKeyEvent>() {
            @Override
            public Class<OnSteelyardKeyEvent> getBusEventClass() {
                return OnSteelyardKeyEvent.class;
            }

            @Override
            public void onReceiver(OnSteelyardKeyEvent data) {
                if (!data.isIntercepted()) {
                    data.setIntercepted(true);

                    if(data.getKeyCode() == OnSteelyardKeyEvent.KEYCODE_PAY){

                        if(RunTimeData.orderEntity == null){
                            ViewUIHelp.showTipsLong(getActivity(),"无订单打印");
                            return;
                        }

                        if (SaleFragment.saleFragment != null) {
                            try {
                                SaleFragment.saleFragment.createPrintInfo(RunTimeData.orderEntity);
                                SaleFragment.saleFragment.printOrder();
                            } catch (Exception e) {
                            }
                        }

                        if (presenter != null) {
                            presenter.printOrder(presenter.createPrintInfo(RunTimeData.orderEntity));
                        }

                    }else {

                        RouterUINavigation.getInstance().dismiss(PaySuccessTipsFragment.this);
                    }
                }
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }
    
    private void regOnWeightHoldEvent(){
        LinkBusCycleWrapper.register(new ObserverAutoNext<OnWeightHoldEvent>() {
            @Override
            public Class<OnWeightHoldEvent> getBusEventClass() {
                return OnWeightHoldEvent.class;
            }
            @Override
            public void onReceiver(OnWeightHoldEvent data) {
                RouterUINavigation.getInstance().dismiss(PaySuccessTipsFragment.this);
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }
    private void loopD() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                try {
                    cjs--;
                    ((TextView) findViewById(R.id.tv_tips)).setText(cjs + "秒后关闭\n打印票据：\"支付打印\";\n关闭界面：按\"任意数字键\"");
                    if (cjs == 0) {
                        RouterUINavigation.getInstance().dismiss(PaySuccessTipsFragment.this);
                    } else {
                        loopD();
                    }
                } catch (Exception e) {
                }
            }
        }, 1 * 1000);
    }

    private int cjs = 30;

    protected class CreateQR extends TaskAdapter<Object[], Void, Bitmap> {

        public CreateQR() {
            super(new CTaskExecutor<Object[], Void, Bitmap>(Config.getTaskExecutor(), Looper.getMainLooper()), CreateQR.class.getName(), MyApplication.getInstance().getTaskManager2());
        }


        @Override
        public void doExecuting(Object[] param) {
            super.doExecuting(param);

            mTaskExecutor.executorDoAsyncExecuting(new RunnablePR<Object[], Void>() {
                @Override
                public Void run(Object[] objects) {
                    complete(onExecuting(objects));
                    return null;
                }
            }, param);
        }

        public Bitmap onExecuting(Object[] param) {

            int ip = -1;
            String orderurl = (String) param[++ip];
            Bitmap bitmap = QRCodeUtil.createImage(orderurl, 400, 400);
            return bitmap;//ResponEntity.fromJson(Api.order_add(c,datas,this),AddOrderResult.class);
        }

        @Override
        public void onFinish(Bitmap bitmap) {
            super.onFinish(bitmap);
            if (isExit()) return;

            String printTable = getArguments().getString("printTable:s");



















            if(printTable==null)printTable="";

            int printTableLength = printTable.length();
            StringBuilder stringBuilder = new StringBuilder();
            int count = 0;
            for (int i = 0; i < printTableLength; i++) {
                String it = printTable.substring(i, i + 1);
                count += SalePrint.getCharWidth(it);
//                System.out.println(SaleFragment.getCharWidth(it)+"     "+it);
                stringBuilder.append(it);
                if (count >= 32) {
                    count = 0;
                    stringBuilder.append("\n");
                }

            }
            String orderInfoStr = stringBuilder.toString();


            try {
                ImageView img_qr = (ImageView) findViewById(R.id.img_qr);
                img_qr.setImageBitmap(bitmap);
            }catch (Exception e){}
            ((TextView) findViewById(R.id.tv_printTable)).setText(orderInfoStr);

        }
    }





//            FunModule steelyardServerModule = AppConst.getModuleManager().loadModule(FunModule.class);








//            FunModule steelyardServerModule = AppConst.getModuleManager().loadModule(FunModule.class);



    @Override
    protected boolean configIsLockKey() {
        
        return !true;
    }
}
